//
//  ZH_MineViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/13.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_MineViewController.h"
#import "ZH_MineTopTableViewCell.h"
#import "ZH_OrderTableViewCell.h"
#import "ZH_MineOtherTableViewCell.h"
#import "ZH_MineInfortionViewController.h"
#import "ZH_MineFocusViewController.h"
#import "ZH_MineOrderViewController.h"
#import "ZH_MineCourseViewController.h"
#import "ZH_TopUpViewController.h"
#import "ZH_MineWalletViewController.h"
#import "ZH_MineInvitationViewController.h"
#import "ZH_MineCreditsViewController.h"
#import "ZH_MineIntegralViewController.h"
#import "ZH_MineSetUpViewController.h"
#import "ZH_StatesOrderViewController.h"
#import "ZH_MineComplaintsViewController.h"
#import "ZH_ApplyRefundViewController.h"
#import "WSShopCarViewController.h"
#import "ZH_ShoppingCarViewController.h"
#import "ZH_MineViewModel.h"
#import "ZH_MineClassViewController.h"
@interface ZH_MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)ZH_MineViewModel *viewModel;
@end

@implementation ZH_MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    [self initData];
}
- (void)loadNavigationBackButton{
    
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initData{
    WEAKSelf;
    [self.viewModel getInformationResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            if (![data[@"data"] isKindOfClass:[NSNull class]]) {
                weakSelf.dic = data[@"data"];
            }
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
///更新导航栏显示
- (void)loadNavigationBarBackground{
    [((Hen_BaseNavigationViewController*)self.navigationController) setNavigationBarBgColor:[UIColor clearColor]];
    [((Hen_BaseNavigationViewController*)self.navigationController) setBackgroundHidden:YES];
    [((Hen_BaseNavigationViewController*)self.navigationController) setShadowViewHidden:YES];
}
- (void)initUserInterface{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_edit"] style:UIBarButtonItemStylePlain target:self action:@selector(mineInfo)];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(-[DATAMODEL KNavigationBarHeioght]);
        make.left.right.bottom.equalTo(self.view);
    }];
}
#pragma mark - private
//我的资料
- (void)mineInfo{
    ZH_MineInfortionViewController *vc = [[ZH_MineInfortionViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 3) {
        return 6;
    }if (section == 4) {
        return 4;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return FITSCALE(9);
    }
    else if (section == 2) {
        return FITSCALE(9);
    }
    else if (section == 3){
        return FITSCALE(9);
    }
    else if (section == 4){
        return FITSCALE(9);
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZH_MineTopTableViewCell *cell = [ZH_MineTopTableViewCell cellWithTableView:tableView];
        cell.backgroundColor = kCommonBackgroudColor;
        [cell updataUI:self.dic];
        WEAKSelf;
        cell.onButtonClick = ^{ //我的关注
            ZH_MineFocusViewController *vc = [[ZH_MineFocusViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }
    else if (indexPath.section == 1){
        ZH_OrderTableViewCell *cell = [ZH_OrderTableViewCell cellWithTableView:tableView];
        cell.backgroundColor = kCommonBackgroudColor;
        cell.rightString = @"查看更多订单 >";
        cell.leftString = @"我的订单";
        cell.leftImages = @"icon_wddd";
        [cell updateUiForData:@[@[@"icon_dfk",@"待付款"],@[@"icon_dfh",@"待发货"],@[@"icon_dsh",@"待收货"],@[@"icon_ysh",@"已收货"],@[@"icon_sh",@"退款/售后"]].mutableCopy];
        WEAKSelf;
        cell.onButtonClick = ^(NSInteger index) {
            ZH_MineOrderViewController *vc = [[ZH_MineOrderViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        
        return cell;
    }
    else if (indexPath.section == 2){
        ZH_OrderTableViewCell *cell = [ZH_OrderTableViewCell cellWithTableView:tableView];
        cell.backgroundColor = kCommonBackgroudColor;
        cell.rightString = @"查看全部课程 >";
        cell.leftString = @"我的课程";
        cell.leftImages = @"icon_wdkc";
        [cell updateUiForData:@[@[@"icon_yykc",@"预约课程"],@[@"icon_offlinekc",@"线下课程"],@[@"icon_livekc",@"直播课程"],@[@"icon_spkc",@"视频课程"],@[@"icon_kcdd",@"课程订单"]].mutableCopy];
        WEAKSelf;
        cell.onButtonClick = ^(NSInteger index) {
            if(index == 4){
                ZH_MineClassViewController *vc = [[ZH_MineClassViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
            else{
                ZH_MineCourseViewController *vc = [[ZH_MineCourseViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        };
        return cell;
    }
    else if (indexPath.section == 3){
        if (indexPath.row == 0) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"购物车";
            cell.leftImageString = @"icon_gwc";
            [cell isHidden:YES];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }
        else if (indexPath.row == 1) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"充值";
            cell.leftImageString = @"icon_jbcz";
            [cell isHidden:YES];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }
        else if (indexPath.row == 2) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"我的钱包";
            cell.leftImageString = @"icon_wdqb";
            [cell isHidden:YES];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }
        else if (indexPath.row == 3) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"邀请有奖";
            cell.leftImageString = @"icon_yqyj";
            [cell isHidden:YES];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }
        else if (indexPath.row == 4) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"我的学分";
            cell.leftImageString = @"icon_wdxf";
            [cell isHidden:YES];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }
        else if (indexPath.row == 5) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"我的积分";
            cell.leftImageString = @"icon_wdjf";
            [cell isHidden:YES];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }
    }
    else if (indexPath.section == 4){
        if (indexPath.row == 0) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"关于我们";
            cell.leftImageString = @"icon_aboutus";
            [cell isHidden:YES];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }
        else if (indexPath.row == 1) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"意见反馈";
            cell.leftImageString = @"icon_aboutus";
            [cell isHidden:YES];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }
        else if (indexPath.row == 2) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"设置";
            cell.leftImageString = @"icon_setup";
            [cell isHidden:YES];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }
        else if (indexPath.row == 3) {
            ZH_MineOtherTableViewCell *cell = [ZH_MineOtherTableViewCell cellWithTableView:tableView];
            cell.leftString = @"客服热线";
            cell.leftImageString = @"icon_yjfk";
            [cell isHidden:NO];
            cell.backgroundColor = kCommonBackgroudColor;
            return cell;
        }

    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 3) {
        if (indexPath.row == 1) { //充值
            ZH_TopUpViewController *vc = [[ZH_TopUpViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.row == 0){//购物车
            ZH_ShoppingCarViewController *vc = [[ZH_ShoppingCarViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
//            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"WSTeacher" bundle:nil];
//            WSShopCarViewController *shopVc = [storyBoard instantiateViewControllerWithIdentifier:[WSShopCarViewController className]];
//            [self.navigationController pushViewController:shopVc animated:YES];
        }
        else if (indexPath.row == 2){//我的钱包
            ZH_MineWalletViewController *vc = [[ZH_MineWalletViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.row == 3){//邀请有奖
            ZH_MineInvitationViewController *vc = [[ZH_MineInvitationViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.row == 4){//我的学分
            ZH_MineCreditsViewController *vc = [[ZH_MineCreditsViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if(indexPath.row == 5){//我的积分
            ZH_MineIntegralViewController *vc = [[ZH_MineIntegralViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    else if (indexPath.section == 4){
        if (indexPath.row == 2) {//设置
            ZH_MineSetUpViewController *vc = [[ZH_MineSetUpViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}
#pragma mark - getter,setter
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITSCALE(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_MineViewModel *)viewModel{
    if (!_viewModel){
        _viewModel = [[ZH_MineViewModel alloc] init];
    }
    return _viewModel;
}
@end
